﻿using System;
using System.Linq;
using System.Reflection;
using PubSub.S3.Abstract;

namespace PubSub.S3.EventName
{
    /// <summary>
    /// 
    /// </summary>
    [AttributeUsage(AttributeTargets.Class)]   
    public class EventNameAttribute : Attribute, IEventNameProvider
    {
        /// <summary>
        /// 事件名称
        /// </summary>
        public string EventName { get; set; }

        public EventNameAttribute(string name)
        {
            this.EventName = name;
        }

        public static string GetNameOrDefault(Type eventType)
        {
            if (typeof(EventDataAbstract).IsAssignableFrom(eventType))
            {
                var attr = eventType.GetCustomAttributes().OfType<IEventNameProvider>().FirstOrDefault();
                if (attr != null)
                    return attr?.GetEventName(eventType);
            }

            return eventType.FullName;
        }

        public string GetEventName(Type eventType)
        {
            return EventName;
        }
    }
}